package com.bodu.system.controller;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.bodu.core.common.AbstractCommonController;
import com.bodu.core.entity.ParamReq;
import com.bodu.core.entity.enums.DelFlagEnum;
import com.bodu.core.entity.result.PageResult;
import com.bodu.core.entity.result.Result;
import com.bodu.global.annon.TraceLog;
import com.bodu.global.security.LoginUserUtil;
import com.bodu.system.entity.request.MenuPermitRequest;
import com.bodu.system.entity.request.PermitRequest;
import com.bodu.system.entity.request.RolePermitRequest;
import com.bodu.system.entity.vo.PermitVo;
import com.bodu.system.service.PermitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 权限管理模块
 *
 * @author r.guo
 * @since 2019-05-25
 */
@Api(tags = "权限管理")
@RestController
@RequestMapping("/permit")
public class PermitController extends AbstractCommonController<PermitService, PermitRequest, PermitVo> {

    private final PermitService permitService;

    public PermitController(PermitService permitService) {
        this.permitService = permitService;
    }

    @TraceLog()
    @ApiOperation("权限详情")
    @GetMapping("{id}")
    public Result getModel(@PathVariable("id") Long id) {
        return super.getModel(permitService, id);
    }

    @TraceLog()
    @ApiOperation("保存权限")
    @PostMapping
    public Result save(@RequestBody PermitRequest permitRequest) {
        if (ObjectUtil.isEmpty(permitRequest.getParentId())) {
            permitRequest.setParentId(-1L);
        }
        return super.save(permitService, permitRequest);
    }

    @TraceLog()
    @ApiOperation("更新权限")
    @PutMapping
    public Result update(@RequestBody PermitRequest permitRequest) {
        if (ObjectUtil.isEmpty(permitRequest.getParentId())) {
            permitRequest.setParentId(-1L);
        }
        return super.update(permitService, permitRequest);
    }

    @TraceLog()
    @ApiOperation("权限列表")
    @PostMapping("listPage")
    public Result<List<PermitVo>> listPage(@RequestBody ParamReq<PermitRequest> req) {
        return super.listPage(permitService, req);
    }

    @TraceLog()
    @ApiOperation("权限统计")
    @PostMapping("countPage")
    public Result<Integer> countPage(@RequestBody ParamReq<PermitRequest> req) {
        return super.countPage(permitService, req);
    }

    @TraceLog()
    @ApiOperation("权限分页")
    @PostMapping("page")
    public Result<PageResult> page(@RequestBody ParamReq<PermitRequest> req) {
        return super.page(permitService, req);
    }

    /**
     * 循环Permit
     */
    @TraceLog()
    @ApiOperation("循环Loop")
    @PostMapping("loopList")
    public Result loopList() {
        List<PermitVo> permitVos = permitService.loopList();
        return Result.success(permitVos);
    }

    @TraceLog()
    @ApiOperation("删除")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Long id) {
        return super.delete(permitService, id);
    }

    @TraceLog()
    @DeleteMapping("{ids}")
    @ApiOperation("批量删除")
    public Result batchDelete(@PathVariable("ids") List<Long> ids) {
        return super.batchDelete(permitService, ids);
    }

    @TraceLog()
    @GetMapping("/listPermitIdByRoleId/{roleId}")
    @ApiOperation("获取permitID")
    public Result listPermitIdByRoleId(@PathVariable("roleId") Long roleId) {
        Map<String, Object> permit = MapUtil.newHashMap();
        permit.put("checkPermit", permitService.listPermitIdByRoleId(roleId));
        permit.put("permitTrees", permitService.loopList());
        return Result.success("查询权限成功！", permit);
    }

    @TraceLog()
    @PostMapping("savePermitByRoleId")
    @ApiOperation("根据Role 保存数据")
    public Result savePermitByRoleId(@Valid @RequestBody RolePermitRequest request) {
        request.setCreateUser(LoginUserUtil.getUserId());
        request.setDelFlag(DelFlagEnum.NO.getValue());
        Integer count = permitService.savePermitByRoleId(request);
        return Result.success("保存permit的数据成功", count);
    }

    @TraceLog()
    @GetMapping("/listPermitIdByMenuId/{menuId}")
    @ApiOperation("获取permitID")
    public Result listPermitIdByMenuId(@PathVariable("menuId") Long roleId) {
        Map<String, Object> permit = MapUtil.newHashMap();
        permit.put("checkPermit", permitService.listPermitIdByMenuId(roleId));
        permit.put("permitTrees", permitService.loopList());
        return Result.success("查询权限成功！", permit);
    }

    @TraceLog()
    @PostMapping("savePermitByMenuId")
    @ApiOperation("根据Role 保存数据")
    public Result savePermitByMenuId(@Valid @RequestBody MenuPermitRequest menuPermitRequest) {
        menuPermitRequest.setCreateUser(LoginUserUtil.getUserId());
        Integer count = permitService.savePermitByMenuId(menuPermitRequest);
        return Result.success("保存permit的数据成功", count);
    }

    @Override
    public Long getLoginUserId() {
        return LoginUserUtil.getUserId();
    }
}
